package net.zjitc.Utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/6/4 14:50
 * @description:
 */
public class MailUtils {

  public static void sendMail(String email, String code) throws Exception {
    //1.创建一个会话session
    Properties prop=new Properties();
    //邮箱服务器
    prop.setProperty("mail.host","smtp.qq.com");
    //授权
    prop.setProperty("mail.smtp.auth","true");
    //qq邮箱需设置端口号
    prop.setProperty("mail.smtp.port","587");

    Authenticator authenticator=new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
        //密码框16位口令
        return new PasswordAuthentication("2932689571@qq.com","apsvzgteexsidfgi");
      }
    };
    Session session=Session.getInstance(prop,authenticator);
    //创建一个代表邮件对象的Message
    Message message=new MimeMessage(session);
    //设置内容
    //发件人地址
    message.setFrom(new InternetAddress("2932689571@qq.com"));
    //收件人地址(TO:直接发送)
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(email));
    //设置标题
    message.setSubject("来自软件");
    //设置发送内容（格式）
    String content = "<a href=\"http://localhost:8080/activeCode.do?code="+code+"\">http://localhost:8080/activeCode.do?code="+code+"</a>";
    message.setContent("请点击下方的链接激活：<br>"+content,"text/html;charset=utf-8");
    //发送
    Transport.send(message);
  }
}
